\section{Memory Management}

\subsection{Physical Memory}

\begin{frame}
  \frametitle{Why temp file as memory?}
  \begin{enumerate}
    \item Any part of the temp file can be mapped to any address.
    \item There's a page cache in the host.
  \end{enumerate}
\end{frame}

\subsection{Memory Layout}

\begin{frame}[containsverbatim]
  \frametitle{Memory Mapping}
\begin{verbatim}
+------------------------------+
|       Host Kernel Area       |
+------------------------------+ <-- 0xC0000000
|        ** Invalid **         |
+------------------------------+ <-- KERNTOP = 0xB8000000
|    Mapped Physical Memory    |                  (128M)
+------------------------------+ <-- KERNBASE / USERTOP
|                              |      = 0xB0000000
:       ~~~~~~~~~~~~~~~        :
|                              |
+------------------------------+ <-- USERBASE = 0x10000
|        ** Invalid **         |
+------------------------------+ <-- 0
\end{verbatim}
\end{frame}

\begin{frame}[containsverbatim]
  \frametitle{Details of Mapped Physical Memory}
\begin{verbatim}
+------------------------------+
|                              |
|         Free Memory          |
|                              |
+------------------------------+
|                              |
|      Kernel Code & Data      |
|                              |
+------------------------------+ <- RESERVED_END
|                              |     = 0xB0100000
|           Reserved           |     (1M reserved space)
|                              |
+------------------------------+ <- KERNBASE = 0xB0000000
\end{verbatim}
\end{frame}

\begin{frame}[containsverbatim]
  \frametitle{Details of Reversed Memory}
\begin{verbatim}
+------------------------------+ <-- RESERVED_END
|   Boot code & Kernel stack   |
+------------------------------+ <-- BOOT_CODE
|                              |      = 0xB00F0000
|     Not used at present      |
|                              |
+------------------------------+
|      Global Infomation       |
+------------------------------+ <-- KERNBASE 
                                   / GLOBL_INFO
\end{verbatim}
\end{frame}

\begin{frame}[containsverbatim]
  \frametitle{Global Info Details}
  Global Info is used to transfer infomation such as memory, normal disk and swap file descriptor in the host and memory map to the kernel. Its definition at present is:

  \begin{lstlisting}
struct global_info {
    int mem_fd;
    int disk_fd; 
    int disk_size;     /* in sector */
    int swap_fd;
    int swap_size;     /* in sector */
    struct E820MAP mem_map;
};
  \end{lstlisting}
\end{frame}

\subsection{Page Table}

\begin{frame}[containsverbatim]
  \frametitle{Page Table Entry}
  Attributes in Process Page Table accessable:
  \begin{center}
    PROT\_READ PROT\_WRITE PROT\_EXEC
  \end{center}

  Page table entry:
  \begin{center}
\begin{verbatim}
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Address          | | | | | |D|A| | |U|W|P|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
\end{verbatim}
  \end{center}
  Address: Offset in the temp file \newline
  D: Dirty Bit \newline
  A: Accessed Bit \newline
  U: User Bit \newline
  W: Writable Bit \newline
  P: Present Bit \newline
\end{frame}

\begin{frame}
  \frametitle{Page Table States}
  \begin{center}
    \begin{tabular}{c|c|c|ccccccc}
      No. & proc PgT & ucore PgT & r & w & ex & m & um \\ \hline
      1 & (noentry) & (noentry) & F & F & F & 2/3 & 1 \\
      2 & [] & [P] & 4 & F & 4 & 2/3 & 1 \\
      3 & [] & [WP] & 5 & 6 & 5 & 2/3 & 1 \\
      4 & [RX] & [AP] & 4 & F & 4 & 2/3 & 1 \\
      5 & [RX] & [AWP] & 5 & 6 & 5 & 2/3 & 1 \\
      6 & [RWX] & [ADWP] & 6 & 6 & 6 & 2/3 & 1 
    \end{tabular}
  \end{center}
  r=read \newline
  w=write \newline
  ex=execute \newline
  m=map(tlb flush) \newline
  um=unmap(tlb invalidate) \newline
  F=Bad Page Fault \newline
\end{frame}

\subsection{Different Checks}

\begin{frame}
  \frametitle{Checks Are A Bit Different}
  Checks of swap, shmem have been modified because:
  \begin{enumerate}
    \item the original address is not accessible \newline
      Map file to address lower than 0x10000 is not allowed on Linux. (Why?) \newline
      As many checks use address 0x0 or 0x1000, they have to be changed so that the checks can carry on normally.
    \item modify cr3 is not permitted. \newline
      The whole user space is flushed each time a new page table takes effect.
  \end{enumerate}
  All else are kept as what they were.
\end{frame}